﻿using StackExchange.Redis;
using System.Collections.Generic;

namespace SCM.Common.Extensions
{
    public interface IRedisHelper
    {
        public void Init(string connStr, int dbNumber);
        IDatabase GetDatabase();
        bool Remove(string key);

        string Get(string key);

        T Get<T>(string key);

        bool Set(string key, string value, int expireSeconds = 0);

        string GetHash(string key, string hashField);

        T GetHash<T>(string key, string hashField);
        RedisValue[] GetHash(string key);


        List<T> GetHashList<T>(string key);

        bool SetHash(string key, string hashField, RedisValue value, int expireSeconds = 0);

        bool RefreshTimeOut(string key, int expireSeconds);

        bool HasKey(string key);

        /// <summary>
        /// 分布式锁
        /// </summary>
        /// <param name="key">锁标识</param>
        /// <param name="token">锁拥有者</param>
        /// <param name="expireMinutes"></param>
        /// <returns></returns>
        public void Lock(string key, string token, int expireSeconds);

        /// <summary>
        /// 检查是否获取成功锁
        /// </summary>
        /// <param name="key"></param>
        /// <param name="token"></param>
        /// <param name="expireSeconds"></param>
        /// <returns></returns>
        public bool LockTake(string key, string token, int expireSeconds);
        /// <summary>
        /// 释放锁
        /// </summary>
        /// <param name="key"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public bool LockRelease(string key, string token);

    }
}
